package codingforgreat.lchot150;

import java.util.Stack;

public class Lc020 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] str = s.toCharArray();
        for(int i = 0;i < str.length;i++){
            if(str[i] == '(' || str[i] == '{' || str[i] == '['){
                stack.push(str[i] == '(' ? ')' : (str[i] == '{' ? '}' : ']') );
            }else{
                if(stack.isEmpty()){
                    return false;
                }else{
                    if(str[i] != stack.pop()){
                        return false;
                    }
                }
            }
        }
        return stack.isEmpty();
    }
}
